package com.lzy.product.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lzy.framework.component.PageBean;
import com.lzy.framework.core.orm.db.Pager;
import com.lzy.framework.core.utils.DozerUtils;
import com.lzy.product.dao.AgentOrderInfoMapperExt;
import com.lzy.product.dtos.agent.AgentOrderInfoDto;
import com.lzy.product.dtos.agent.AgentOrderSalesRecordDto;
import com.lzy.product.entity.AgentOrderInfo;
import com.lzy.product.service.AgentOrderInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;

@Service
public class AgentOrderInfoServiceImpl implements AgentOrderInfoService {

    @Autowired
    private AgentOrderInfoMapperExt agentOrderInfoMapperExt;


    @Override
    public Long insert(AgentOrderInfoDto dto) {
        AgentOrderInfo agentOrderInfo = DozerUtils.map(dto, AgentOrderInfo.class);
        agentOrderInfoMapperExt.insertSelective(agentOrderInfo);
        return agentOrderInfo.getOrderId();
    }

    @Override
    public Boolean update(AgentOrderInfoDto dto) {
        return agentOrderInfoMapperExt.updateByPrimaryKeySelective(DozerUtils.map(dto, AgentOrderInfo.class)) > 0 ? true : false;
    }

    @Override
    public Boolean delete(Long id) {
        return agentOrderInfoMapperExt.deleteByPrimaryKey(id) > 0 ? true : false;
    }

    @Override
    public Boolean remove(Long id) {
        return null;
    }

    @Override
    public AgentOrderInfoDto findById(Long id) {
        return DozerUtils.map(agentOrderInfoMapperExt.selectByPrimaryKey(id), AgentOrderInfoDto.class);
    }

    @Override
    public List<AgentOrderInfoDto> query(AgentOrderInfoDto dto) {
        return agentOrderInfoMapperExt.query(dto);
    }

    @Override
    public PageBean selectList(Pager pager, AgentOrderInfoDto agentOrderInfoDto) {
        if (null != pager) {
            PageHelper.startPage(pager.getPageNum(), pager.getPageSize());
        }
        List<AgentOrderInfoDto> agentOrderInfoDtoList = agentOrderInfoMapperExt.query(agentOrderInfoDto);
        return new PageBean(new PageInfo<>(agentOrderInfoDtoList).getTotal(), agentOrderInfoDtoList);
    }

    @Override
    public AgentOrderInfoDto findByOrderId(Long orderId) {
        return DozerUtils.map(agentOrderInfoMapperExt.selectByPrimaryKey(orderId), AgentOrderInfoDto.class);
    }

    @Override
    public Boolean updateAgentOrder(AgentOrderInfoDto dto) {
        return agentOrderInfoMapperExt.updateByPrimaryKeySelective(DozerUtils.map(dto, AgentOrderInfo.class)) > 0;
    }

    @Override
    public BigDecimal queryTotalPriceByUserId(Long userId) {
        return agentOrderInfoMapperExt.queryTotalPriceByUserId(userId);
    }

    @Override
    public PageInfo<AgentOrderSalesRecordDto> statisticalSalesRecord(Pager pager, AgentOrderInfoDto agentOrderInfoDto) {
        if (null != pager) {
            PageHelper.startPage(pager.getPageNum(), pager.getPageSize());
        }
        return new PageInfo<>(agentOrderInfoMapperExt.statisticalSalesRecord(agentOrderInfoDto));
    }

    @Override
    public List<AgentOrderSalesRecordDto> statisticalSalesRecordByCondition(String idList, Integer type, String time) {
        return agentOrderInfoMapperExt.statisticalSalesRecordByCondition(idList, type, time);
    }
    @Override
    public AgentOrderInfoDto queryContractByOrderId(Long orderId){
        return agentOrderInfoMapperExt.queryContractByOrderId(orderId);
    }
}
